package com.compuvest.admin.chart;

import java.io.IOException;
import java.io.PrintWriter;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/compuvestchart/getChartData")
public class ChartData extends HttpServlet {

    private final static String VERSION = "ChartData v2.0 2007-13-02";
    private ChartMySQL mysql;

    @Override
    public void init(ServletConfig config) throws ServletException {
	super.init(config);

	mysql = new ChartMySQL();

	System.out.println("Init: " + VERSION);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

	String xml = "";
	String userID = request.getParameter("userID");
	String stockDate = request.getParameter("stockDate");
	if(userID != null && userID.length() != 0) {
    	    xml = mysql.getUserStock(userID);
	}
	else if(stockDate != null && stockDate.length() != 0) {
    	    xml = mysql.getStock(stockDate);
	}
	else {
    	    xml = mysql.getStock(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
//    	    xml = mysql.getStock("2007-08-01");
	}
//        xml = testData1();
//	System.out.println(xml);

        response.setContentType("text/xml; charset=UTF-8");
        try(PrintWriter out = response.getWriter()) {
            out.print(xml);
        }
    }

   public String testData1() {

        StringBuilder result = new StringBuilder();
        result.append("<?xml version='1.0' encoding='UTF-8' ?>");
        result.append("<Chart>");

        result.append("<User name='201' date='2007-09-20'>");
        result.append("<Days_30>100</Days_30>");
        result.append("<Days_60>200</Days_60>");
        result.append("<Days_90>300</Days_90>");
        result.append("<Days_180>400</Days_180>");
        result.append("<Days_240>500</Days_240>");
        result.append("</User>");

        result.append("<User name='210' date='2007-09-20'>");
        result.append("<Days_30>1000</Days_30>");
        result.append("<Days_60>2000</Days_60>");
        result.append("<Days_90>3000</Days_90>");
        result.append("<Days_180>4000</Days_180>");
        result.append("<Days_240>5000</Days_240>");
        result.append("</User>");

        result.append("</Chart>");

        return result.toString();
    }

   // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "Chart Data";
    }

    @Override
    public void destroy() {
        mysql = null;

        System.out.println("Destroy: " + VERSION);
    }// </editor-fold>
}
